#include "listoperation.h"

ListOperation::ListOperation()
{

}
double ListOperation::getMeanValue(QList<double>l)
{
    int len=l.size();
    if(len>0)
    {
        double sum=0;
        for (int var = 0; var < len; var++) {
            sum+=l.at(var);
        }
        return sum/len;

    }else
    {
        return 0;
    }
}
/******************************************************************************
**功能:数组的最小值索引
**输入参数:double形数组
**输出参数:最小值索引
**作者:
**日期:
**参考:
*******************************************************************************/
int ListOperation::ListMin_Index(QList<double>l)
{
    int IndexTemp=0;
    if(l.length()==0) return 0;
    else
    {
        double ValTemp=l.at(0);
        for(int i=1;i<l.length();i++)
        {
            if(ValTemp>l.at(i))
            {
                ValTemp=l.at(i);
                IndexTemp=i;
            }
        }
        return IndexTemp;
    }
}

QList<double> ListOperation::ListSub(QList<double>List,int startIndex,int ListLength)
{
    QList <double>l;
    for(int i=0;i<qMin(ListLength,List.length()-startIndex);i++)
    {
        l.insert(i,List.at(i+startIndex));
    }
    return l;
}

QList<double>ListOperation::ListDiff(QList<double>list)
{
    QList<double> temp;
    if(list.length()<2)
    {
        return list;
    }else
    {
        for(int i=0;i<list.length()-1;i++)
        {
            temp.insert(i,list.at(i+1)-list.at(i));
        }
        return temp;
    }
}
QList<int>ListOperation::ListDiff(QList<int>list)
{
    QList<int> temp;
    if(list.length()<2)
    {
        return list;
    }else
    {
        for(int i=0;i<list.length()-1;i++)
        {
            temp.insert(i,list.at(i+1)-list.at(i));
        }
        return temp;
    }
}
QList<double> ListOperation::SubList(QList<double>l,int index,int len)
{
    QList<double>temp;
    int len1=l.length()-index;
    if(len>len1){
        len=len1;
    }
    for(int i=index;i<index+len;i++)
    {
        temp.append(l.at(i));
    }
    return temp;
}
int ListOperation::ListMax_Index(QList<double>l)
{
    if(l.length()==0) return 0;
    else
    {
        int IndexTemp=0;
        double ValTemp=l.at(0);
        for(int i=1;i<l.length();i++)
        {
            if(ValTemp<l.at(i))
            {
                ValTemp=l.at(i);
                IndexTemp=i;
            }
        }
        return IndexTemp;
    }

}
